<?php
/**
 * desc Created by wang.
 * User: wang
 * motto: 是时候为小时候吹过的牛而努力了!
 * Date: 2020/3/23 16:20
 */

namespace app\admin\business;


use app\common\lib\Arr;
use app\common\model\mysql\Auth as AuthModel;
use app\common\model\mysql\Role as RoleModel;
use think\Exception;

class Auth extends AdminBase
{
    /**
     * 功能说明 添加权限功能
     * @author: wang
     * @Date: 2020/3/24 18:08
     * @param $data
     * @return bool
     * @throws Exception
     */
    public static function add($data)
    {
        $AuthModel = new AuthModel();
        try {
            $res = $AuthModel->save($data);
        } catch (\Exception $e) {
            throw new \think\Exception('内部异常');
        }

        //记录日志
        self::insert_admin_log('添加了【' . $data['title'] . '菜单】信息');
        return $res;
    }

    /**
     * 功能说明 通过ID更新数据
     * @author: wang
     * @Date: 2020/3/24 18:12
     * @param $data
     * @return mixed
     * @throws Exception
     */
    public static function edit($id, $data)
    {
        $id = intval($id);
        if (empty($id)) {
            throw new \think\Exception('Id错误');
        }
        $AuthModel = new AuthModel();
        try {
            $res = $AuthModel->edit($id, $data);
        } catch (\Exception $e) {
            throw new \think\Exception('内部异常');
        }

        //记录日志
        self::insert_admin_log('更新了【' . $data['title'] . '菜单】信息');
        return $res;
    }

    /**
     * 功能说明 通过ID删除数据
     * @author: wang
     * @Date: 2020/3/24 18:20
     * @param $id
     * @return bool
     * @throws Exception
     */
    public static function delDataById($id)
    {
        $AuthModel = new AuthModel();
        try {
            $res = $AuthModel->delDataById($id);
        } catch (\Exception $e) {
            throw new \think\Exception('内部异常');
        }

        //记录日志
        self::insert_admin_log('删除了ID为-【' . $id . '】的菜单信息');
        return $res;
    }

    /**
     * 功能说明 通过ID获取数据
     * @author: wang
     * @Date: 2020/3/24 17:28
     * @param $id
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getDataById($id)
    {
        $id = intval($id);
        if (empty($id)) {
            return [];
        }
        $AuthModel = new AuthModel();
        return $AuthModel->getDataById($id);
    }


    /**
     * 功能说明 获取权限列表
     * @author: wang
     * @Date: 2020/3/24 16:38
     * @param string $limit
     * @return \think\Paginator
     */
    public static function getAuthList($limit = '5000')
    {
        $AuthModel = new AuthModel();
        $res = $AuthModel->getAuthList($limit)->toArray();
        $nav = new Arr();
        $data = $nav->rule($res['data']);
        unset($res['data']);
        $res['data'] = $data;
        return $res;
    }

    /**
     * 功能说明 通过ID更新排序字段
     * @author: wang
     * @Date: 2020/3/24 16:46
     * @param $data
     * @return bool
     * @throws Exception
     */
    public static function upSort($data)
    {
        $AuthModel = new AuthModel();
        try {

            $res = $AuthModel->upSort($data);
        } catch (\Exception $e) {
            throw new \think\Exception('内部异常');
        }

        //记录日志
        self::insert_admin_log('更新ID为-【' . $data['id'] . '菜单】的排序：(' . $data['sort'] . ')');
        return $res;
    }


    /**
     * 功能说明 权限检测
     * @author: wang
     * @Date: 2020/4/1 14:28
     * @param $url
     * @param $rid
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function check($url, $rid)
    {
        $RoleModel = new RoleModel();
        $ids = $RoleModel->getDataById($rid)->toArray();
        $rules = [];
        if (empty($ids['rules'])) {
            return false;
        }
        $rules = explode(',', $ids['rules']);

        $AuthModel = new AuthModel();
        $myAuth = $AuthModel->getAuthByIds($rules)->toArray();
        $auths = array_column($myAuth, 'name');
        if (in_array($url, $auths)) {
            return true;
        }
        return false;
    }

    /**
     * 功能说明 获取个人权限
     * @author: wang
     * @Date: 2020/4/1 19:55
     * @param $rid
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getMyAuth($rid)
    {
        $AuthModel = new AuthModel();
        if($rid != 1){
            $RoleModel = new RoleModel();
            $ids = $RoleModel->getDataById($rid)->toArray();
            $rules = [];
            if (empty($ids['rules'])) {
                return [];
            }
            $rules = explode(',', $ids['rules']);
            $myAuth = $AuthModel->getAuthByIds($rules)->toArray();
        }else{
            $where[]= ['type', '=', 1];
            $myAuth = $AuthModel->where($where)->order('sort asc')->select()->toArray();
            foreach ($myAuth as $k => $v){
                $rules[] = $v['id'];
            }
        }
        $nav = new Arr();
        $data = $nav->rule($myAuth);
        $res = $nav->arrToAuthArr($data, $rules);
        return $res;

    }



}